package com.foxmail.yusp.mycloud;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;

public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnDismissListener {

    private int id;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 取当前时间
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        Bundle bundle = getArguments();
        id = bundle.getInt("id");

        // 创建并返回TimePickerDialog
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        if (view.isShown()) {

            EditText editText = getActivity().findViewById(id);
            editText.setText(String.format("%02d:%02d", hourOfDay, minute));
        }

    }

// end class
}